<template>
	<view>
		<u-toast ref="uToast" />
		<u-top-tips ref="uTips"></u-top-tips>
		<!-- 头部会员卡 -->
		<view class="top_box">
			<view class="statistics">
				<view class="total">
					5593人已开通会员
				</view>
				<view class="buyquery">
					购买查询
				</view>
			</view>
			<view class="top_card">
				<image v-if="userInfo.avatarUrl" :src="userInfo.avatarUrl" mode="widthFix"></image>
				<image v-else src="/static/icons/avatar.png" mode="widthFix"></image>
				<view>
					<view v-if="userInfo.nickName">
						<text>{{userInfo.nickName}}</text>
						<text class="identity">会员</text>
					</view>
					<view v-else>
						<text>立即登录</text>
						<text v-if="userInfo.vip" class="identity">会员</text>
						<text v-else-if="userInfo" class="identity">普通用户</text>
					</view>
					<view>开通会员尊享VIP权益</view>
				</view>
			</view>
		</view>
		<!-- 套餐选择 -->
		<view class="package_box">
			<view v-for="(item,index) in packageData" :key="item._id" :class="{active: current == index}"
				@click="changPackage(index)">
				<view class="package_mouth">
					{{item.mouth}}个月
				</view>
				<view class="package_salePrice">
					￥{{item.salePrice}}
				</view>
				<view class="package_normalPrice">
					￥{{item.normalPrice}}
				</view>
			</view>
		</view>
		<!-- 立即开通 -->
		<view class="open_box">
			<view class="discount">
				<view>恭喜</view>
				<text>您获得2折开通会员特权</text>
			</view>
			<view>
				<u-popup v-model="show" mode="bottom" height="600">
					<view class="pay_box">
						<view class="pay_title">开通VIP会员</view>
						<view class="pay_list">
							<view v-for="(item,index) in packageData" :key="item._id"
								:class="{active: current == index}" @click="changPackage(index)">
								<view class="package_mouth">
									{{item.mouth}}个月
								</view>
								<view class="package_salePrice">
									￥{{item.salePrice}}
								</view>
								<view class="package_normalPrice">
									￥{{item.normalPrice}}
								</view>
							</view>
						</view>
						<view class="buy_btn">
							<view class="buy_discount">
								<view>恭喜</view>
								<text>开通后立即全站去广告、免费看1000+名厨视频</text>
							</view>
							<button type="default" @click="buyNow">立即购买</button>
						</view>
					</view>
				</u-popup>
				<u-button @click="openNow">立即开通</u-button>
			</view>
		</view>
		<!-- VIP特权 -->
		<view class="privilege">
			<view class="privilege_title">
				VIP特权
			</view>
			<view class="privilege_item" v-for="item in privilegeData" :key="item">
				<!-- EFCA83 -->
				<view class="privilege_item_image">
					<image :src="item.icon" mode=""></image>
				</view>
				<view class="content">
					<view class="privilege_item_title">
						{{item.name}}
					</view>
					<view class="privilege_item_des">
						{{item.des}}
					</view>
				</view>
			</view>
		</view>
		<!-- 会员使用说明 -->
		<view class="instructions">
			<view class="instructions_title">
				会员使用说明
			</view>
			<view class="" v-for="(item,index) in instructions" :key="item">
				{{index+1}}.{{item}}
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: null,
				vipData: {
					_id: '',
					date: ''
				},
				packageData: [],
				current: 0,
				show: false,
				privilegeData: [{
						icon: '/static/icons/book.png',
						name: '1000+精品名厨菜谱',
						des: '大咖教学，轻松学会'
					},
					{
						icon: '/static/icons/new.png',
						name: '每周上新',
						des: '新菜持续更新中'
					},
					{
						icon: '/static/icons/noAdvise.png',
						name: '会员免广告打扰',
						des: '体验更流畅'
					},
					{
						icon: '/static/icons/f-vip.png',
						name: 'VIP尊贵身份标识',
						des: '随时随地，与众不同'
					},
				],
				instructions: ["会员服务一经开通，不支持退款；", "如遇到苹果手机支付问题，建议参考Apptore支付流程", "若会员开通出现异常，请立即联系客服，我们会在2个工作日内出处理结果。",
					"会员常见问题、会员服务协议、会员隐私协议。"
				]
			}
		},
		methods: {
			changPackage(index) {
				this.current = index;
			},
			topupList() {
				uni.getStorage({
					key: 'token',
					success: async ({
						data
					}) => {
						const result = await this.$api.member.topupList(data);
						if (result.meta.status == 200) {
							this.packageData = result.data;
						}
						console.log(result);
					}
				})
			},
			openNow() {
				if (this.userInfo) {
					this.show = true;
				} else {
					this.$refs.uToast.show({
						title: '请先登录',
						type: 'warning',
						url: '/pages/login/login'
					})
				}
			},
			buyNow() {
				this.vipData._id = this.userInfo._id
				this.vipData.date = this.packageData[this.current].mouth;
				uni.getStorage({
					key: 'token',
					success: async ({
						data
					}) => {
						const result = await this.$api.member.openVip(data, this.vipData);
						if (result.code) {
							this.$refs.uTips.show({
								title: result.msg,
								type: 'success',
								duration: '2300'
							})
							this.show = false;
							uni.navigateTo({
								url: `/pages/member/payCompleted?price=${this.packageData[this.current].salePrice}`
							})
						}
					}
				})

			}
		},
		onShow: function() {
			this.userInfo = getApp().globalData.userInfo;
			this.topupList();
		}
	}
</script>

<style lang="scss">
	.top_box {
		position: relative;
		background: #313131;
		margin-bottom: 150rpx;
		height: 300rpx;
		border-radius: 0 0 100rpx 100rpx;

		.statistics {
			display: flex;
			justify-content: space-between;
			padding: 20rpx 0;
			color: #FFFFFF;

			.total {
				background: #5C5C5C;
				padding: 10rpx 20rpx;
				border-radius: 0 30rpx 30rpx 0;
			}

			.buyquery {
				padding: 0 30rpx;
			}
		}

		.top_card {
			margin: 0 auto;
			display: flex;
			width: 700rpx;
			height: 300rpx;
			background: #FAE3B1;
			padding: 50rpx 30rpx;
			border-radius: 20rpx;

			image {
				border-radius: 50%;
				width: 120rpx;
				border: 8rpx solid #fff;
			}

			>view {
				>view {

					&:first-child {
						color: #7B5B21;
						margin: 20rpx;
						font-size: 30rpx;
						font-weight: 700;

						.identity {
							margin-left: 20rpx;
							background: #A5A5A5;
							color: #fff;
							font-weight: 400;
							padding: 0 30rpx;
							border-radius: 50rpx;
						}
					}

					&:last-child {
						color: #7B5B21;
						margin: 20rpx;
					}
				}
			}
		}
	}

	.package_box {
		display: flex;
		justify-content: space-around;

		>view {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 220rpx;
			height: 240rpx;
			border: 2rpx solid transparent;
			border-radius: 20rpx;

			.package_mouth {
				font-size: 30rpx;
				font-weight: 500;
			}

			.package_salePrice {
				font-size: 50rpx;
				color: #DEB264;
			}

			.package_normalPrice {
				color: #A49FA0;
				text-decoration: line-through;
			}

		}

		.active {
			position: relative;
			border-color: #DCB66A;

			&::after {
				content: '推荐';
				position: absolute;
				top: -20rpx;
				left: 0;
				padding: 0 20rpx;
				clip-path: polygon(0 0, 100% 0%, 85% 100%, 0% 100%);
				border-radius: 10rpx;
				color: #fff;
				background: #DEB062;
			}
		}
	}

	.open_box {
		margin: 30rpx;

		.discount {
			display: flex;
			margin: 20rpx 0;

			>view {
				padding: 0 30rpx;
				background: #FBE1AF;
				color: #DEB264;
				margin-right: 10rpx;
				clip-path: polygon(15% 0%, 100% 0%, 85% 100%, 0% 100%);
			}

			>text {
				color: #7B5B21;
			}
		}

		.pay_box {
			margin-top: 30rpx;
			overflow: hidden;

			.pay_title {
				margin: 30rpx 0;
				text-align: center;
			}

			.pay_list {
				display: flex;
				justify-content: space-around;

				>view {
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					width: 220rpx;
					height: 240rpx;
					border: 2rpx solid transparent;
					border-radius: 20rpx;

					.package_mouth {
						font-size: 30rpx;
						font-weight: 500;
					}

					.package_salePrice {
						font-size: 50rpx;
						color: #DEB264;
					}

					.package_normalPrice {
						color: #A49FA0;
						text-decoration: line-through;
					}

				}

				.active {
					position: relative;
					border-color: #DCB66A;

					&::after {
						content: '即将涨价';
						position: absolute;
						top: -20rpx;
						left: 0;
						padding: 0 20rpx;
						clip-path: polygon(0 0, 100% 0%, 85% 100%, 0% 100%);
						border-radius: 10rpx;
						color: #fff;
						background: #E64044;
					}
				}
			}

			.buy_btn {
				width: 700rpx;
				margin: 0 auto;

				.buy_discount {
					display: flex;
					align-items: center;
					margin: 20rpx 0;

					>view {
						padding: 0 30rpx;
						background: #FBE1AF;
						color: #DEB264;
						margin-right: 10rpx;
						clip-path: polygon(15% 0%, 100% 0%, 85% 100%, 0% 100%);
					}

					>text {
						font-size: 24rpx;
						color: #000;
					}
				}

				button {
					background: #FCF1D3;
					color: #A98C4F;
				}
			}
		}
	}

	.privilege {
		width: 700rpx;
		margin: 0 auto;

		.privilege_title {
			font-size: 40rpx;
			margin: 50rpx 0;
		}

		.privilege_item {
			display: flex;
			margin: 10rpx 0;

			.privilege_item_image {
				width: 120rpx;
				height: 120rpx;
				background: #FBF3E7;
				border-radius: 50%;
				overflow: hidden;
				display: flex;
				justify-content: center;
				align-items: center;

				>image {
					width: 50%;
					height: 50%;
				}
			}

			.content {
				margin: 20rpx 0 20rpx 20rpx;

				.privilege_item_title {
					font-size: 36rpx;
					margin-bottom: 10rpx;
				}

				.privilege_item_des {
					color: #ccc;
					font-size: 30rpx;
				}
			}
		}
	}

	.instructions {
		width: 700rpx;
		margin: 30rpx auto;
		color: #ccc;

		.instructions_title {
			font-size: 30rpx;
			color: #000;
			margin: 20rpx 0;
		}
	}
</style>
